package com.liangzhong.model.mail
{
	import com.liangzhong.events.AppEventDispatcher;
	import com.liangzhong.events.NotifierEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.type.MailType;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 *邮件管理 
	 * @author lake
	 * 
	 */	
	public class MailDataManager extends EventDispatcher
	{
		/**
		 *邮件列表 
		 */		
		public var mailList 	:Array = [];
		/**
		 *未领取数量 
		 */		
		private var _unGetNum 	:int;
		
		public function MailDataManager()
		{
			/*var vo :MailVo = new MailVo();
			vo.ntype = MailType.GUESS;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			vo = new MailVo();
			vo.ntype = MailType.GUESS;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			vo = new MailVo();
			vo.ntype = MailType.MATCH;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			vo = new MailVo();
			vo.ntype = MailType.TASK;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			vo = new MailVo();
			vo.ntype = MailType.GUESS;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			vo = new MailVo();
			vo.ntype = MailType.MATCH;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			vo = new MailVo();
			vo.ntype = MailType.TASK;
			vo.time = new Date().time / 1000;
			vo.bget = 1;
			mailList.push(vo);
			sortData();*/
		}
		/**
		 *获取设置邮件未领取数量 
		 * @param value
		 * 
		 */		
		public function set unGetNum(value :int):void
		{
			_unGetNum = value;
			if(_unGetNum < 0)
			{
				_unGetNum = 0;
			}
			if(_unGetNum > 320)
			{
				_unGetNum = 320;
			}
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MAIL_NUM_CHANGE,_unGetNum);
		}
		public function get unGetNum():int
		{
			return _unGetNum;
		}
		/**
		 *数据排序 
		 * 
		 */		
		private function sortData():void
		{
			mailList.sortOn(["bget","time"],[Array.NUMERIC | Array.DESCENDING,Array.NUMERIC | Array.DESCENDING]);
		}
		/**
		 *解析邮件列表 
		 * @param objList
		 * 
		 */		
		public function parseMailList(objList :Array):void
		{
			if(objList == null || objList.length == 0)
			{
				return;
			}
			mailList.splice(0);
			_unGetNum = 0;
			var mailVo :MailVo;
			for each(var obj :Object in objList)
			{
				mailVo = new MailVo();
				mailVo.analysisData(obj);
				if(mailVo.ntype == 0)continue;
				mailList.push(mailVo);
				if(mailVo.bget == 2 && mailVo.ntype == MailType.AWARD)
				{
					_unGetNum ++;
				}
			}
			sortData();
			this.dispatchEvent(new Event(Event.CHANGE));
			AppEventDispatcher.getInstance().dispatchEvt(NotifierEvent.MAIL_NUM_CHANGE,_unGetNum);
		}
		/**
		 * 获取邮件列表通过类型 
		 * @param type
		 * @return 
		 */		
		public function getMailListByType(type :int):Array
		{
			if(MailType.ALL == type)
			{
				mailList.sortOn(["bget","time"],[Array.NUMERIC | Array.DESCENDING,Array.NUMERIC | Array.DESCENDING]);
				return mailList;
			}
			var res :Array = [];
			for each(var mailVo :MailVo in mailList)
			{
				if(mailVo.ntype == type)
				{
					res.push(mailVo);
				}
			}
			switch(type)
			{
				case MailType.AWARD://奖励
				{
					res.sortOn(["bget","time"],[Array.NUMERIC | Array.DESCENDING,Array.NUMERIC | Array.DESCENDING]);
					break;
				}
				default:
				{
					res.sortOn("time",Array.NUMERIC | Array.DESCENDING);
				}
			}
			return res;
		}
		/**
		 *添加邮件 
		 * @param vo
		 * 
		 */		
		public function addMail(vo :MailVo):void
		{
			deleteMailById(vo.mailid);
			mailList.push(vo);
			if(vo.bget == 2 && vo.ntype == MailType.AWARD)
			{
				unGetNum ++ ;
			}
			sortData();
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 *删除邮件通过id 
		 * @param id
		 * 
		 */		
		public function deleteMailById(id :int):void
		{
			var i :int = 0;
			for each(var vo :MailVo in mailList)
			{
				if(vo.mailid == id)
				{
					mailList.splice(i,1);
					break;
				}
				i ++ ;
			}
			this.dispatchEvent(new Event(Event.CHANGE));
		}
	}
}